% Analysing Pirce reaction to jumps and proce clustering following jumps
% Ensure inputs are valid
if length(jumps) ~= 120 || length(prices) ~= 120
    error('Both jumps and prices must be vectors of length 120.');
end

% Initialize variables
num_months = 120;
reaction_window = 3; % Number of months to analyze after a jump
price_reactions = []; % To store price changes after jumps
price_clusters = []; % To store clustering metric after jumps

% Analyze price reactions to jumps
for i = 1:(num_months - reaction_window)
    if jumps(i) > 0 % Consider only positive jumps (adjust as needed)
        % Calculate price change over the reaction window
        price_change = prices(i + 1:i + reaction_window) - prices(i);
        price_reactions = [price_reactions; price_change];
    end
end

% Compute summary statistics
mean_reaction = mean(price_reactions, 1);
std_reaction = std(price_reactions, 0, 1);

% Display results
fprintf('Average price reaction over 3 months after jumps:\n');
for t = 1:reaction_window
    fprintf('Month %d: Mean = %.4f, Std Dev = %.4f\n', t, mean_reaction(t), std_reaction(t));
end

% Plot the reactions
figure;
errorbar(1:reaction_window, mean_reaction, std_reaction, 'o-', 'LineWidth', 1.5);
grid on;
title('Average Price Reaction Over 3 Months After Jumps');
xlabel('Months After Jump');
ylabel('Price Change');

% ----------- Analyse Price Clustering -------------------

% Analyze price clustering following jumps
for i = 1:(num_months - reaction_window)
    if jumps(i) > 0 % Consider only positive jumps (adjust as needed)
        % Calculate price changes over the reaction window
        price_changes = prices(i + 1:i + reaction_window) - prices(i);
        % Calculate clustering metric (e.g., standard deviation of price changes)
        clustering_metric = std(price_changes);
        price_clusters = [price_clusters; clustering_metric];
    end
end

% Compute summary statistics
mean_clustering = mean(price_clusters);
std_clustering = std(price_clusters);

% Display results
fprintf('Price clustering metrics following jumps:\n');
fprintf('Mean clustering: %.4f\n', mean_clustering);
fprintf('Standard deviation of clustering: %.4f\n', std_clustering);

% Plot clustering metrics
figure;
histogram(price_clusters, 10, 'FaceColor', 'b', 'EdgeColor', 'k');
grid on;
title('Histogram of Price Clustering Metrics Following Jumps');
xlabel('Clustering Metric (Std Dev of Price Changes)');
ylabel('Frequency');